﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AjaxData_Topic_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string act=Request["action"];
        if (!string.IsNullOrEmpty(act))
        {
            switch (act)
            {
                case "topic_save":
                    var topic_bl = new Com.Ooohd.Bll.Topic.ZS_Topic();
                    var topic = topic_bl.CreateModel();
                    string id = Request["id"];
                    if (!string.IsNullOrEmpty(id)) {
                        topic = topic_bl.GetSingle(int.Parse(id));
                    }
                    topic.Title = Request["title"];
                    topic.StartTime = DateTime.Parse(Request["starttime"]);
                    topic.EndTime = DateTime.Parse(Request["endtime"]);
                    topic.PreviewImg = Request["previewImg"];
                    topic.Profile = Request["profile"];
                    topic.Detail=Request["content"];
                    if (!string.IsNullOrEmpty(id))
                    {
                        topic.ID = int.Parse(id);
                        topic_bl.Update(topic);
                    }
                    else
                    {
                        topic.CreateDate = DateTime.Now;
                        topic_bl.Add(topic);
                    }
                    Response.Write(Common.JSONHelper.ObjectToJSON(new { succ = true, id = topic.ID }));
                    break;
                 
                case "topic_get":
                    topic_bl = new Com.Ooohd.Bll.Topic.ZS_Topic();
                    topic = topic_bl.GetSingle(int.Parse(Request["id"]));
                    topic.Title += "^" + topic.StartTime.ToString() + "^" + topic.EndTime.ToString();
                    Response.Write(Common.JSONHelper.ObjectToJSON(topic));
                    break;
                 
                case "topic_del":
                    topic_bl = new Com.Ooohd.Bll.Topic.ZS_Topic();
                    topic_bl.Delete(Request["ids"]);
                    Response.Write(Common.JSONHelper.ObjectToJSON(new { succ = true }));
                    break;
                case "album_add":
                    var topic_album_bl = new Com.Ooohd.Bll.Topic.ZS_TopicAlbum();
                    var topic_album = topic_album_bl.CreateModel();
                    topic_album.AlbumName = Request["albumname"].Trim();
                    topic_album.CreateDate = DateTime.Now;

                    if (!string.IsNullOrEmpty(Request["albumid"]))
                    {
                        var img_bl = new Com.Ooohd.Bll.Img.ZS_Image();
                        var img_list = img_bl.Query("albumid=@0", "", int.Parse(Request["albumid"]));
                        var topicImgList = new List<Com.Ooohd.Model.Topic.ZS_TopicImg>();
                        foreach (var item in img_list)
                        {
                            topicImgList.Add(new Com.Ooohd.Model.Topic.ZS_TopicImg { ImgName = item.ImgName, CreateDate = DateTime.Now, ImgUrl = "http://img.ds568.net/album/a" + item.AlbumID + "/" + item.FileName });
                        }
                        
                        topic_album_bl.Add(topic_album, topicImgList);
                    }
                    else {
                        topic_album_bl.Add(topic_album);
                    }
                    Response.Write(Common.JSONHelper.ObjectToJSON(new { succ = true }));
                    break;
                case "album_del":
                    topic_album_bl = new Com.Ooohd.Bll.Topic.ZS_TopicAlbum();
                    topic_album_bl.Delete(Request["ids"]);
                    Response.Write(Common.JSONHelper.ObjectToJSON(new { succ = true }));
                    break;
                case "img_add":
                    var topic_img_bl = new Com.Ooohd.Bll.Topic.ZS_TopicImg();
                    var topic_img = topic_img_bl.CreateModel();
                    topic_img.ImgName=Request["imgname"];
                    topic_img.ImgUrl=Request["imgurl"];
                    topic_img.AlbumID = int.Parse(Request["alb_id"]);
                    topic_img.CreateDate = DateTime.Now;
                    topic_img_bl.Add(topic_img);
                    Response.Write(Common.JSONHelper.ObjectToJSON(new { succ = true }));
                    break;
                case "img_del":
                    topic_img_bl = new Com.Ooohd.Bll.Topic.ZS_TopicImg();
                    topic_img_bl.Delete(Request["ids"]);
                    Response.Write(Common.JSONHelper.ObjectToJSON(new { succ = true }));
                    break;
            }
        }
    }
}